package com.common.oss.instance;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.common.oss.config.OssConfig;
import com.common.oss.exception.OssBaseException;
import com.common.oss.exception.OssExceptionMsg;
import org.springframework.stereotype.Component;

@Component
public class OssInstance {

    private OSS ossClient ;

    private String bucketName;

    private String accessPre ;

    public OssInstance(OssConfig ossConfig) {
        ossClient = new OSSClientBuilder().build(
                ossConfig.getEndPoint(),
                ossConfig.getAccessKeyId(),
                ossConfig.getAccessKeySecret());
        this.bucketName = ossConfig.getBucketName();
        this.accessPre = ossConfig.getAccessPre();
    }

    public String getBucketName() {
        return bucketName;
    }

    public String getAccessPre() {
        return accessPre;
    }

    public OSS getOssClient() {
        if (ossClient == null){
            throw new OssBaseException(OssExceptionMsg.OSS_FAIL_MSG);
        }
        return ossClient;
    }

    public void ossShutdown(){
        if (ossClient == null){
            throw new OssBaseException(OssExceptionMsg.OSS_FAIL_MSG);
        }
        ossClient.shutdown();
    }

}
